﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComicHelper.Controller;
using ComicSpider;

namespace ComicHelper
{
    public partial class MainForm : Form
    {
        public MainFormController Controller
        {
            get;
            set;
        }

        public MainForm()
        {
            InitializeComponent();
            //init
            MainForm_Init();
        }

        public void MainForm_Init()
        {
            this.button_Save.Enabled = false;
            this.checkBox_All.Enabled = false;
            this.panel_List.HorizontalScroll.Enabled = false;
        }

        private int GetAnalyzeSiteNum()
        {
            int siteNum = 0;
            foreach (RadioButton radnBtn in this.panel_Site.Controls)
            {
                if (radnBtn.Checked)
                {
                    switch (radnBtn.TabIndex)
                    {
                        case 10:
                            siteNum = 1;
                            break;
                        case 11:
                            siteNum = 2;
                            break;
                        case 12:
                            siteNum = 3;
                            break;
                        case 13:
                            siteNum = 4;
                            break;
                    }
                }
            }

            return siteNum;
        }

        private void button_Analyze_Click(object sender, EventArgs e)
        {
            this.button_Save.Enabled = true;
            this.checkBox_All.Enabled = true;
            int num = this.GetAnalyzeSiteNum();
            if (num == 0)
            {
                MessageBox.Show("请选择漫画站点！");
            }
            else
            {
                if (textBox_Site.Text == string.Empty)
                {
                    MessageBox.Show("漫画地址不可为空！");
                }
                else
                {
                    this.Controller.SiteNum = num - 1;
                    this.Controller.GetComicSiteBookList(this.textBox_Site.Text);
                }
            }
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            this.Controller.SaveSelectedComicBook();
            this.button_Analyze.Enabled = false;
            //StringBuilder strbuff = new StringBuilder();
            //for (int i = 0; i < 50; i++)
            //{
            //    CheckBox check = panel_List.Controls[i] as CheckBox;
            //    if (check.Checked)
            //    {
            //        strbuff.AppendLine(check.Text);
            //    }
            //}

            //if (strbuff.Length != 0)
            //    MessageBox.Show(strbuff.ToString());
        }

        private void checkBox_All_CheckedChanged(object sender, EventArgs e)
        {
            foreach (CheckBox box in panel_List.Controls)
            {
                box.Checked = checkBox_All.Checked;
            }
        }

        private void button_Folder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog_Save.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowserDialog_Save.Description = "请选择文件夹";
            DialogResult result = folderBrowserDialog_Save.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox_Folder.Text = folderBrowserDialog_Save.SelectedPath;
            }
            else
                textBox_Folder.Text = "请选择目录！";
        }
    }
}
